package com.usermanage.controller;

import com.contract.Bean.ContractBean1;
import com.contract.Common.ContractIOC;
import com.contract.Common.TablePriceToCatalogIOC;
import com.ordercore.Util.ResponseData.ResponseData;
import com.pinza.Common.SkuIOC;
import com.usermanage.util.HttpUtil;
import com.usermanage.util.JsonData;
import com.usermanage.util.StaticUtil;
import com.usermanage.util.StringUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 根据公司ID查询产品库（手机端）
 */
@RestController
@RequestMapping("/product")
public class IndexController {

    @RequestMapping("/treeList.json")
    public String tree(String company_id) throws IOException {
        if ("1".equals(company_id)) {
            String result = HttpUtil.getDemo(StaticUtil.SKUIP + "/pzCfgCataLogController/pzCfgCataLogTree.json");
            return result;
        } else {
            ContractBean1 contractBean1 = new ContractBean1();
            contractBean1.setCompanyB_id(company_id);
            ResponseData responseData = ContractIOC.findOne(contractBean1);
            if (responseData.isRet()) {
                ContractBean1 contractBean11 = (ContractBean1) responseData.getData();
                Integer contract_id1 = Integer.valueOf(contractBean11.getId());
                Integer priceSummaryId1 = Integer.valueOf(contractBean11.getTablePrice_id());
                return StringUtil.ObjectToJson(TablePriceToCatalogIOC.getCatalogByCompanyId(contract_id1, priceSummaryId1));
            } else {
                JsonData jsonData = new JsonData(false);
                return StringUtil.ObjectToJson(jsonData);
            }
        }
    }

    @RequestMapping("/spuList.json")
    public String spu(String cls,String company_id) throws IOException {
        if ("1".equals(company_id)) {
            String result = HttpUtil.getDemo(StaticUtil.SKUIP + "/spuCatalogRelateConntroller/getAllRelate.json?page=0&limit=0&param="+cls);
            return result;
        } else {
            ContractBean1 contractBean1 = new ContractBean1();
            contractBean1.setCompanyB_id(company_id);
            ResponseData responseData = ContractIOC.findOne(contractBean1);
            if (responseData.isRet()) {
                ContractBean1 contractBean11 = (ContractBean1) responseData.getData();
                Integer contract_id1 = Integer.valueOf(contractBean11.getId());
                Integer priceSummaryId1 = Integer.valueOf(contractBean11.getTablePrice_id());
                return StringUtil.ObjectToJson(TablePriceToCatalogIOC.getSpuByCls3(contract_id1, priceSummaryId1,cls));
            } else {
                JsonData jsonData = new JsonData(false);
                return StringUtil.ObjectToJson(jsonData);
            }
        }
    }

    @RequestMapping("/skuList.json")
    public String sku(String cls,String company_id,int page,int limit,String spu_code,String key) throws IOException {
        if ("1".equals(company_id)) {
            String result = HttpUtil.getDemo(StaticUtil.SKUIP + "/sku/getSkuListPageBySpuCode.json?page="+page+"&limit="+limit+"&spuCode="+spu_code+"&key="+key);
            return result;
        } else {
            ContractBean1 contractBean1 = new ContractBean1();
            contractBean1.setCompanyB_id(company_id);
            ResponseData responseData = ContractIOC.findOne(contractBean1);
            if (responseData.isRet()) {
                ContractBean1 contractBean11 = (ContractBean1) responseData.getData();
                Integer contract_id1 = Integer.valueOf(contractBean11.getId());
                return StringUtil.ObjectToJson(ContractIOC.contractSkuList(page, limit, contract_id1, spu_code, cls, key));
            } else {
                JsonData jsonData = new JsonData(false);
                return StringUtil.ObjectToJson(jsonData);
            }
        }
    }

    @RequestMapping("skuInfo.json")
    public String skuInfo(String sku_code,String company_id) throws IOException {
        if ("1".equals(company_id)) {
            String result = HttpUtil.getDemo(StaticUtil.SKUIP + "/sku/detailSku.json?sku_code="+sku_code);
            return result;
        } else {
          return StringUtil.ObjectToJson(SkuIOC.skuDetail(sku_code));
        }
    }
}
